home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 5 / Apprentice-Release5.iso / Source Code / C++ / Applications / NeuroSim 1.0 / .cp / CParamsDialog.cp < prev    next >
Text File  |  1996-02-19  |  7KB  |  237 lines

  1. // ===========================================================================
  2. //    CParamsDialog.cp            ©1996 Timo Eloranta
  3. // ===========================================================================
  4. //    This class handles the dialog where the user can view and modify the
  5. //    parameters of a new neural net. CParamsDialog is derived from LDialogBox - 
  6. //    a PowerPlant window class with default (OK) and Cancel buttons.
  7.  
  8. #include "CParamsDialog.h"
  9.  
  10. #include <PP_Messages.h>        // cmd_About
  11. #include <UDesktop.h>
  12.  
  13. #include "NS_Utils.h"            // SignedIntField keyfilter
  14.  
  15. // ---------------------------------------------------------------------------
  16. //        • CParamsDialog
  17. //
  18. //          Called by:    CParamsDialog::CreateParamsDialogStream
  19. // ---------------------------------------------------------------------------
  20. //    Constructor. Call the base class with the LStream object reference.
  21.  
  22. CParamsDialog::CParamsDialog(
  23.     LStream *inStream )
  24.         : LDialogBox( inStream )
  25. {
  26. }
  27.  
  28. // ---------------------------------------------------------------------------
  29. //        • CreateParamsDialogStream [static]
  30. //
  31. //          Called by:    CNeuroSimApp::ObeyCommand (indirect call)
  32. // ---------------------------------------------------------------------------
  33. //    Return a new ParamsDialog object initialized using data from a Stream.
  34.  
  35. CParamsDialog *
  36. CParamsDialog::CreateParamsDialogStream(
  37.     LStream *inStream)
  38. {
  39.     return ( new CParamsDialog( inStream ) );
  40. }
  41.  
  42. // ---------------------------------------------------------------------------
  43. //        • InitDialog
  44. //
  45. //          Called by:    CNeuroSimApp::ObeyCommand
  46. // ---------------------------------------------------------------------------
  47. //    Initialize all the controls inside the dialog.
  48.  
  49. void
  50. CParamsDialog::InitDialog()
  51. {
  52.     mSizeCapt        = (LCaption *)     this -> FindPaneByID( capt_Size );
  53.     mSizeSlider        = (HorzSlider *) this -> FindPaneByID( slid_Size );
  54.         
  55.     mMinEdit        = (LEditField *) this -> FindPaneByID( edit_Qty_Min );
  56.     mMaxEdit        = (LEditField *) this -> FindPaneByID( edit_Qty_Max );
  57.     mAvg_X_Edit        = (LEditField *) this -> FindPaneByID( edit_Avg_x );
  58.     mDev_X_Edit        = (LEditField *) this -> FindPaneByID( edit_Dev_x );
  59.     mAvg_Y_Edit        = (LEditField *) this -> FindPaneByID( edit_Avg_y );
  60.     mDev_Y_Edit        = (LEditField *) this -> FindPaneByID( edit_Dev_y );
  61.  
  62.     mFactoryButton    = (LStdButton *) this -> FindPaneByID( but_Factory );
  63.  
  64.     if ( mFactoryButton)
  65.         mFactoryButton -> AddListener( this );
  66.  
  67.     mMinEdit -> Enable();
  68.     mMinEdit -> Activate();
  69.     mMinEdit -> SelectAll();
  70.  
  71.     if ( mSizeSlider ) {
  72.         mSizeSlider -> SetPicts( pict_Base, pict_Slider, pict_Selected );
  73.         mSizeSlider -> SetMinMax( 2, 14 );
  74.         mSizeSlider -> SetValueMessage( slid_Size );
  75.  
  76.         if ( mSizeSlider -> MakeSlider() )
  77.             mSizeSlider -> AddListener( this );
  78.          else
  79.              delete mSizeSlider;
  80.      }
  81.      
  82.      // The average values can be negative and since PowerPlant
  83.      // doesn't have a built-in keyfilter for allowing only
  84.      // numbers and the '-' character, we set our custom keyfilter
  85.      // here "manually". The filter is defined in NS_Utils.h.
  86.      
  87.      mAvg_X_Edit -> SetKeyFilter( SignedIntField );
  88.      mAvg_Y_Edit -> SetKeyFilter( SignedIntField );
  89. }
  90.  
  91. // ---------------------------------------------------------------------------
  92. //        • SetValues
  93. //
  94. //          Called by:    CNeuroSimApp::ObeyCommand
  95. //                        CParamsDialog::SetDefaultValues
  96. // ---------------------------------------------------------------------------
  97. //    Set the controls of the dialog to the values given in "inParams".
  98.  
  99. void
  100. CParamsDialog::SetValues( SGenParams &inParams )
  101. {
  102.     SetSizeValue( inParams.size );
  103.     mSizeSlider -> SetSliderValue( inParams.size );
  104.  
  105.     mMinEdit    -> SetValue( inParams.qtyMin );
  106.     mMaxEdit    -> SetValue( inParams.qtyMax );
  107.     mAvg_X_Edit -> SetValue( inParams.xLengthAvg );
  108.     mDev_X_Edit -> SetValue( inParams.xLengthDev );
  109.     mAvg_Y_Edit -> SetValue( inParams.yLengthAvg );
  110.     mDev_Y_Edit -> SetValue( inParams.yLengthDev );
  111. }
  112.  
  113. // ---------------------------------------------------------------------------
  114. //        • SetSizeValue
  115. //
  116. //          Called by:    CParamsDialog::SetValues
  117. //                        CParamsDialog::ListenToMessage
  118. // ---------------------------------------------------------------------------
  119. //    Transform the given (numeric) net size N to a string of type "N x N" 
  120. //    and set this string to the caption field beside the slider.
  121.  
  122. void
  123. CParamsDialog::SetSizeValue( Int16 inValue )
  124. {
  125.     LStr255        theNbrString( (Int32) inValue );
  126.     LStr255        theString( theNbrString );
  127.  
  128.     theString.Append( "\p x " ); 
  129.     theString.Append( theNbrString ); 
  130.  
  131.     mSizeCapt    -> SetDescriptor( theString );
  132. }
  133.  
  134. // ---------------------------------------------------------------------------
  135. //        • GetValues
  136. //
  137. //          Called by:    CNeuroSimApp::ObeyCommand
  138. // ---------------------------------------------------------------------------
  139. //    Read the new values of the controls. Do also some validity checking.
  140.  
  141. void
  142. CParamsDialog::GetValues( SGenParams &outParams )
  143. {
  144.     outParams.size            = mSizeSlider -> GetSliderValue();
  145.  
  146.     outParams.xLengthAvg    = mAvg_X_Edit -> GetValue();
  147.     outParams.xLengthDev    = mDev_X_Edit -> GetValue();
  148.     outParams.yLengthAvg    = mAvg_Y_Edit -> GetValue();
  149.     outParams.yLengthDev    = mDev_Y_Edit -> GetValue();
  150.  
  151.     outParams.qtyMin        = mMinEdit -> GetValue();
  152.     outParams.qtyMax        = mMaxEdit -> GetValue();
  153.     
  154.     // Minimum can not be bigger than maximum...
  155.     // If it is, set the maximum to be equal to minimum
  156.     // and show an alert box to the silly user!
  157.     
  158.     if ( outParams.qtyMax < outParams.qtyMin ) {
  159.         outParams.qtyMax = outParams.qtyMin;
  160.         LStr255    theParam0( (Int32) outParams.qtyMin );
  161.         
  162.         UDesktop::Deactivate();
  163.         ::ParamText( theParam0, "\p", "\p", "\p");
  164.         ::StopAlert( ALRT_MinMax, nil );
  165.         UDesktop::Activate();
  166.     }
  167. }
  168.  
  169. // ---------------------------------------------------------------------------
  170. //        • ListenToMessage
  171. //
  172. //          Called by:    LBroadcaster::BroadcastMessage
  173. // ---------------------------------------------------------------------------
  174. //    Respond to messages from Broadcasters
  175.  
  176. void
  177. CParamsDialog::ListenToMessage(
  178.     MessageT    inMessage, 
  179.     void        *ioParam )
  180. {
  181.     switch ( inMessage ) {
  182.  
  183.         case slid_Size:
  184.             if ( mSizeCapt ) {
  185.                 SetSizeValue( *(Int32 *) ioParam );
  186.                 mSizeCapt -> Draw( NULL );
  187.             }
  188.             break;
  189.             
  190.         case msg_FactorySettings:
  191.             SetDefaultValues();
  192.             break;
  193.             
  194.         default:    
  195.             LDialogBox::ListenToMessage( inMessage, ioParam );
  196.             break;
  197.     }
  198. }
  199.  
  200. // ---------------------------------------------------------------------------
  201. //        • SetDefaultValues
  202. //
  203. //          Called by:    CParamsDialog::ListenToMessage
  204. // ---------------------------------------------------------------------------
  205. //    This function is used when the user pushes the "Factory Settings" button.
  206.  
  207. void
  208. CParamsDialog::SetDefaultValues()
  209. {
  210.     SGenParams theDefaults = {    DEFAULT_LENGTH_X_AVG,    DEFAULT_LENGTH_Y_AVG,
  211.                                 DEFAULT_LENGTH_X_DEV,    DEFAULT_LENGTH_Y_DEV,
  212.                                 DEFAULT_NET_SIZE,
  213.                                 DEFAULT_QTY_MIN,        DEFAULT_QTY_MAX };
  214.     SetValues( theDefaults );
  215. }
  216.         
  217. // ---------------------------------------------------------------------------
  218. //        • FindCommandStatus
  219. //
  220. //          Called by:    LCommander::ProcessCommandStatus
  221. // ---------------------------------------------------------------------------
  222. //    Disable all menu commands except cmd_About
  223.  
  224. void
  225. CParamsDialog::FindCommandStatus(
  226.     CommandT    inCommand,
  227.     Boolean        &outEnabled,
  228.     Boolean&    /* outUsesMark */,
  229.     Char16&        /* outMark */,
  230.     Str255        /* outName */)
  231. {
  232.     outEnabled = false;
  233.     if (inCommand == cmd_About) {
  234.         outEnabled = true;
  235.     }
  236. }
  237.